package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"

	"library"
	"mp"
)

var lib *library.MusicManager
var id int = 1
var ctrl, signal chan int

func handleLibCommands(tokens []strings) {
	switch tokensp[1] {
	case "list":
		for i := 0; i < lib.Len(); i++ {
			e, _ := lib.Get(i)
			fmt.Println(i+1, ":", e.Name, e.Artist, e.Source, e.Type)

		}
	case "add":
		{
			if len(tokens) == 6 {
				id++
				lib.Add(&library.MusicEntry(strconv.Itoa(id)),
					tokens[2], tokens[3], tokens[4], tokens[5])

			} else {
				fmt.Println("usage:lib add <name> <artist> <source> <type>")
			}
		}
	case "remove":
		if len(tokens) == 3 {
			lib.RemoveByName(tokens[2])

		} else {
			fmt.Println("usage: lib remove <name>")
		}
	default:
		fmt.Println("unrecognized lib command :", tokens[1])
	}

}

func handlePlayCommand(tokens []string) {
	if len(tokens) != 2 {
		fmt.Println("usage: paly <name>")
		return
	}
	e := lib.Find(tokens[1])
	if e == nil {
		fmt.Println("The music ", tokens[1], "does not exist")
	}
	mp.Play(e.Source, e.Type, ctrl, signal)

}

func main() {
	fmt.Println(`Enter follwing command to control the player:
		lib list --  View the existing music lib 
		lib add <name> <artist><source><type> -- Add amusic to the music lib 
		lib remove <name> -- Remove the specified music from the lib play <name> -- Play the specified music
		`)
	lib = library.NewMusicManager()
	r := bufio.NewReader(os.Stdin)

	for i := 0; i <= 100; i++ {
		fmt.Print("Enter command-> ")
		rawLine, _, _ := r.ReadLine()

		line := string(rawLine)
		if line == "q" || line == "e" {
			break
		}
		tokens := strings.Split(line, " ")

		if tokens[0] == "lib" {
			handleLibCommands(tokens)
		} else if tokens[0] == "play" {
			handlePlayCommands(tokens)
		} else {
			fmt.Println("Unrecognized command :", tokens[0])
		}
	}
}
